﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataBase.Orden;
using Model.Orden;

using System.Collections;
using System.Collections.ObjectModel;

namespace Processes.Orden
{
    public  class GestorOrden
    {
        public bool setAsociada(string nro)
        {
            if (DBOrden.setAsociada(nro))
                return true;
            return false;
        }
        public Order NroOrdenNoFueAsociada()
        {
            try 
	        {
                Order orden = DBOrden.NroOrdenNoFueAsociada();
                return orden;
	        }
	        catch (Exception ex)
	        {
		
		        throw ex;
	        }
        }
        public bool SetOrden(DateTime fecha, string nro,List<Punto> puntos)
        {
            try
            {
                bool ok = false;
                ok=DBOrden.SetFecha(fecha,nro);
                ok= DBPunto.ModificarPuntos(nro,puntos);
                if (ok)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }
        public DateTime FechaDeNumero(string nro)
        {
            try
            {
                return DBOrden.FechaDeNumero(nro);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public bool ExisteOrdenEnEsaFecha(DateTime time)
        {
            try
            {
                return DBOrden.ExisteOrdenEnEsaFecha(time);
            }
            catch (Exception e)
            {
                throw e;
            }
            

        }
        public bool EstaAprobada(string nro)
        {
            try
            {
                if (DBOrden.EstaAprobada(nro))
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
            
        }

        public bool Aprobar(string nro)
        {
            try
            {
                return DBOrden.Aprobar(nro);
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }
        public Order BuscarOrdenSinAprobar()
        {
            try
            {
                Order orden = DBOrden.ExisteOrdenSinAprobar();
                return orden;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public Collection<Order> buscarOrdenNro(String Nro, String Anio,bool secre)
        {
            Nro += "-" + Anio;
            return DBOrden.buscarOrdenNro(Nro,secre);
        }
        public Collection<Order> buscarOrdenFecha(DateTime ini, DateTime fin,bool secre)
        {
            return DBOrden.buscarOrdenFecha(ini, fin,secre);
        }
        public bool InsertarOrden(Order orden,List<Punto> punto)
        { 
            if(DBOrden.insertOrder(orden,punto))
                return true;
            return false;
        }
        public List<Punto> puntosDeNro(string nro)
        {
            return DataBase.Orden.DBPunto.puntosDeNro(nro);
        }

        public bool canCreateOrder()
        {
            if (DBOrden.canCreateOrder())
                return true;
            else
                return false;
        }
        public string nextNro()
        {
            string result = "";
            result = DBOrden.lastNro();
            if (result != "")
            {
                string[] word = result.Split('-');
                int number = int.Parse(word[0]);
                int year = int.Parse(word[1]);
                int nextnumber;
                int nextYear;

                if (year == DateTime.Now.Year)
                {
                    nextYear = year;
                    nextnumber = number+1;
                }
                else
                {
                    nextnumber = 1;
                    nextYear = year+1;
                }
                result = nextnumber + "-" + nextYear;
            }
            else
            {
                result = "1-" + DateTime.Now.Year;
            }
            return result;
        }
    }
}
